﻿using Microsoft.AspNetCore.Mvc;
using yunbo.Model.BI;
using yunbo.Repositorys.BIRepositorys;

namespace yunbo.Controllers.BIControllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class PPController : Controller
	{
		PPRepository ppRepository = new PPRepository();


		/// <summary>
		/// 获取品牌
		/// </summary>
		/// <param name="state"></param>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet("GetPP")]
		public IActionResult GetPP(int state, string? str)
		{
			List<PP> pp = ppRepository.GetPP(state, str);
			return Ok(pp);
		}


		/// <summary>
		/// 绑定品牌下拉框
		/// </summary>
		/// <returns></returns>
		[HttpGet("GetOnePP")]
		public IActionResult GetOnePP()
		{
			List<PP> pp = ppRepository.GetOnePP();
			return Ok(pp);
		}


		/// <summary>
		/// 改变状态
		/// </summary>
		/// <param name="id"></param>
		/// <param name="state"></param>
		/// <returns></returns>
		[HttpPut("StateChange")]
		public IActionResult StateChange(int id, int state)
		{
			bool res = ppRepository.StateChange(id, state);
			return Ok(res);
		}


		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="pp"></param>
		/// <returns></returns>
		[HttpPost("AddPP")]
		public IActionResult AddPP(PP pp)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(pp.pp_code)
				|| string.IsNullOrEmpty(pp.pp_name)
				|| string.IsNullOrEmpty(pp.pp_jc)
				|| string.IsNullOrEmpty(pp.pp_ywmc)
				|| string.IsNullOrEmpty(pp.pp_ks)
				)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = ppRepository.AddPP(pp);
			return Ok(res);
		}


		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="pp"></param>
		/// <returns></returns>
		[HttpPut("UpdatePP")]
		public IActionResult UpdatePP(PP pp)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(pp.pp_code)
				|| string.IsNullOrEmpty(pp.pp_name)
				|| string.IsNullOrEmpty(pp.pp_jc)
				|| string.IsNullOrEmpty(pp.pp_ywmc)
				|| string.IsNullOrEmpty(pp.pp_ks)
				|| pp.pp_id <= 0
				)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = ppRepository.UpdatePP(pp);
			return Ok(res);
		}


		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelPP")]
		public IActionResult DelPP(int id)
		{
			bool res = ppRepository.DelPP(id);
			return Ok(res);
		}


		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelPPs")]
		public IActionResult DelPPs(int[] ids)
		{
			bool res = ppRepository.DelPPs(ids);
			return Ok(res);
		}

	}
}
